const {
  rootPath,
  API,
  HomePath
} = require("../../utils/const")
const {
  postLogin,
  getRequest,
  requestUserInfo
} = require("../../utils/request")

const dateUtils = require("../../utils/dateUtils")

Page({
  data: {
    complaint: {}, // 用于存储投诉详情的数据
    complaintDoctors: [],
    complaintDoctorName: ''
  },

  onLoad: function(options) {
    const complaintId = options.id; // 获取传递的投诉ID
    this.fetchComplaintDetail(complaintId);  
  },

  fetchComplaintDetail: function(id) {
    const that = this;
    getRequest(API.getComplaintById + '/' + id).then(res => {
      that.setData({
        complaint: res
      }, () => {
        const complaint = that.data.complaint;
        complaint.complaintTime = dateUtils.formatComplaintTime(complaint.complaintTime);
        complaint.handleTime = dateUtils.formatComplaintTime(complaint.handleTime);
        that.setData({
          complaint: complaint
        })
      });

      getRequest(API.getDoctorList).then(resplus => {
        that.setData({
          complaintDoctors: resplus
        });

        const selectedDoctor = that.data.complaintDoctors.find(doctor => doctor.doctorId === that.data.complaint.doctorId);

        that.setData({
          complaintDoctorName: selectedDoctor.doctorName
        });
      });
    });
  }
});